import { ChangeDetectionStrategy, Component, OnInit, ViewChild } from '@angular/core';
import { PageHeaderModule } from '@delon/abc/page-header';
import { STColumn, STModule } from '@delon/abc/st';
import { _HttpClient } from '@delon/theme';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Api } from 'src/app/shared/api';
import { ResponseCode } from 'src/app/shared/response.code';
import { NzDownloadModalComponent } from 'src/app/shared/widget/download.widget';

@Component({
  selector: 'app-unknown',
  templateUrl: './unknow.component.html',
  changeDetection: ChangeDetectionStrategy.Default,
  standalone: true,
  imports: [PageHeaderModule, STModule, NzIconModule]
})
export class UnknownComponent implements OnInit {
  page: any = { show: false };
  data: any[] = [];
  nodeLevel: any[] = [];
  fileType: { [key: string]: any } = {
    folder: 'folder',
    excel: 'file-excel',
    word: 'file-word',
    ppt: 'file-ppt',
    pdf: 'file-pdf',
    image: 'file-image',
    gif: 'file-gif',
    video: 'video-camera',
    sound: 'sound',
    text: 'file-text',
    zip: 'file-zip',
    unknown: 'file-unknown'
  };
  constructor(
    private http: _HttpClient,
    private msg: NzMessageService
  ) {}

  ngOnInit() {
    this.getUserNodeByTypes();
  }

  columns: STColumn[] = [
    { render: 'type', width: 50 },
    { title: '文件名', index: 'name', sort: true },
    {
      width: 200,
      buttons: [
        {
          icon: 'download',
          iif: record => this.canDownload(record),
          type: 'modal',
          modal: {
            params: (record: any) =>
              (record = { url: `${Api.BaseNodeApi}/download?id=${record.id}`, filename: record.path ? record.name : `${record.name}.zip` }),
            component: NzDownloadModalComponent,
            modalOptions: { nzClosable: false, nzMask: true, nzTitle: '下载中……', nzMaskClosable: false },
            size: 'sm'
          }
        },
        // {
        //   icon: 'eye',
        //   iif: record => this.canPreview(record),
        //   click: record => this.preview(record)
        // },
        {
          icon: 'delete',
          type: 'del',
          iif: record => this.canEdit(record),
          click: record => this.delete(record)
        }
      ]
    },
    { title: '大小', render: 'size', width: 150, sort: true }
    // { title: '拥有者', index: 'owner' ,width: 150, sort: true},
  ];

  getUserNodeByTypes() {
    let params = { types: ['unknown'] };
    this.http.get(`${Api.BaseNodeApi}/node/type`, params).subscribe(res => {
      if (res.code === ResponseCode.SUCCESS) {
        this.data = res.data;
      } else {
        this.msg.error(res.message ? res.message : '获取失败');
      }
    });
  }

  canDownload(record: any): boolean {
    // TODO 判断下载权限
    return true;
  }

  canPreview(record: any): boolean {
    // TODO 判断预览权限
    if (record.type === 'folder') {
      return false;
    }
    return true;
  }

  canEdit(record: any): boolean {
    // TODO 判断编辑权限
    return true;
  }

  getFileType(type: string): string {
    const icon = this.fileType[type];
    return icon ? icon : this.fileType['unknown'];
  }

  getFileSize(size: number): string {
    if (size) {
      if (size > 1024 * 1024 * 1024 * 1024) {
        return `${(size / 1024 / 1024 / 1024 / 1024).toFixed(2)}TB`;
      } else if (size > 1024 * 1024 * 1024) {
        return `${(size / 1024 / 1024 / 1024).toFixed(2)}GB`;
      } else if (size > 1024 * 1024) {
        return `${(size / 1024 / 1024).toFixed(2)}MB`;
      } else if (size > 1024) {
        return `${(size / 1024).toFixed(2)}KB`;
      } else {
        return `${size}B`;
      }
    }
    return '-';
  }

  rowChange(event: any) {
    if (event && event.type === 'sort') {
      console.log(`按照${event.sort.column.indexKey}进行${event.sort.value}排序`);
    }
    console.log(event);
  }

  preview(record: any) {
    this.msg.warning('请对接预览服务');
  }

  delete(record: any) {
    let params = { id: record.id };
    this.http.delete(Api.BaseNodeApi, params).subscribe(res => {
      if (res.code === ResponseCode.SUCCESS) {
        this.msg.success('删除成功');
        if (this.nodeLevel.length > 0) {
          this.getUserNodeByTypes();
        } else {
          this.getUserNodeByTypes();
        }
      } else {
        this.msg.error(res.message ? res.message : '删除失败！');
      }
    });
  }
}
